<?php
/**
 * @package 	mod_bt_contentshowcase - BT ContentShowcase Module
 * @version		2.0
 * @created		Oct 2011

 * @author		BowThemes
 * @email		support@bowthems.com
 * @website		http://bowthemes.com
 * @support		Forum - http://bowthemes.com/forum/
 * @copyright	Copyright (C) 2012 Bowthemes. All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');
$document = JFactory::getDocument();
$document->addStyleSheet(JURI::root() . "modules/mod_bt_contentshowcase/tmpl/css/default.btcontentshowcase.css");
$document->addStyleSheet(JURI::root() . "modules/mod_bt_contentshowcase/tmpl/css/jcarousel.css");
// ROW * COL
$itemsPerRow = (int)$params->get( 'items_per_rows', 3 );
$itemsPerCol = (int)$params->get( 'items_per_cols', 1 );
$vertical = $params->get('slide_direction') != 'vertical' ? false : true;
//Num of item display
//$maxPages = $itemsPerRow*$itemsPerCol;
if($vertical){
    if($itemsPerRow == 1) $itemPerLi = 1;
    else $itemPerLi = $itemsPerRow;
}else{
    if($itemsPerCol == 1) $itemPerLi = 1;
    else $itemPerLi = $itemsPerCol;
}

//Get pages list array
$pages = array_chunk( $list, $itemPerLi  );

//Get total pages
$totalPages = count($pages);

// calculate width of each row.
$liWidth = $params->get('module_width');
$liHeight = $params->get('item_height') +20;
$itemWidth = 100;

if(!$vertical){
    $liWidth = number_format(100/$itemsPerRow,2) * $params->get('module_width') /100 -10;
    $liHeight = ($params->get('item_height') + 20) * $itemsPerCol;
}else{
    $itemWidth = number_format(100/$itemsPerRow,2);
}

?>

<?php if(count($list)>0) :?>
<div style="width:<?php echo $moduleWidthWrapper;?>">

	<div id="btcontentshowcase<?php echo $module->id; ?>"  class="bt-contentshowcase<?php echo $moduleclass_sfx ? ' bt-contentshowcase'.$params->get('moduleclass_sfx'):'';?>">
		
		<?php 
			$add_style = "";
			if( trim($params->get('content_title')) ){
			$add_style= "border: 1px solid #CFCFCF;padding:10px 0px;";
		?>
		<h3>
                    <?php if($params->get('content_title_link')) {?>
                        <a href="<?php echo $params->get('content_title_link');?>"><span><?php echo $params->get('content_title') ?> </span></a>
                    <?php } else { ?>
                        <span><?php echo $params->get('content_title') ?> </span>                    
                    <?php   }?>
                        
		</h3>
		<?php } ?>
		<div  style="width:<?php echo $moduleWidth.";".$add_style;?>">
                    <ul class="jcarousel jcarousel-skin-tango">
                        <?php foreach( $pages as $key => $list ): ?>
                        <?php //class="'. (($i==0) ? 'bt-row-first' : (($i==count($list)-1) ? 'bt-row-last' : '')) . '"?> 
                        <?php echo '<li>'?>
                        <?php foreach( $list as $i => $row ): ?>
                            
                            <div class="bt-row " style="width: <?php echo $itemWidth?>%;">
					<div class="bt-inner">
                                        <?php if($row->thumbnail && $align_image != 'center'){?>
                                            <div style="float: <?php echo $align_image ;?>;">
						<a target="<?php echo $openTarget; ?>"
							class="bt-image-link"
							title="<?php echo $row->title;?>" href="<?php echo $row->link;?>">
							<img <?php echo $imgClass ?> src="<?php echo $row->thumbnail; ?>" alt="<?php echo $row->title?>"  style=" height:<?php echo $thumbHeight ;?>px; width:<?php echo $thumbWidth ;?>px;" title="<?php echo $row->title?>" />
						</a>
                                            </div>
                                        <?php } ?>
					<?php if( $show_category_name ): ?>
					<?php if($show_category_name_as_link) : ?>
						<a class="bt-category" target="<?php echo $openTarget; ?>"
							title="<?php echo $row->category_title; ?>"
							href="<?php echo $row->categoryLink;?>"> <?php echo $row->category_title; ?>
						</a>
						<?php else :?>
						<span class="bt-category"> <?php echo $row->category_title; ?> </span>
						<?php endif; ?>
						<?php endif; ?>

						<?php if( $showTitle ): ?>
						<a class="bt-title" target="<?php echo $openTarget; ?>"
							title="<?php echo $row->title; ?>"
							href="<?php echo $row->link;?>"> <?php echo $row->title_cut; ?> </a>
							<?php endif; ?>
							<?php if( $row->thumbnail ): ?>
                                                <?php if($row->thumbnail && $align_image == 'center') {?>
						<div style="text-align:center">
						<a target="<?php echo $openTarget; ?>"
							class="bt-image-link"
							title="<?php echo $row->title;?>" href="<?php echo $row->link;?>">
							<img <?php echo $imgClass ?> src="<?php echo $row->thumbnail; ?>" alt="<?php echo $row->title?>"  style=" height:<?php echo $thumbHeight ;?>px; width:<?php echo $thumbWidth ;?>px;" title="<?php echo $row->title?>" />
						</a>
						</div>
                                                <?php } ?>
						<?php endif ; ?>
						<?php if( $showAuthor || $showDate ): ?>
						<div class="bt-extra">
						<?php if( $showAuthor ): ?>
							<span class="bt-author"><?php 	echo JText::sprintf('BT_CREATEDBY' ,
							JHtml::_('link',JRoute::_($row->authorLink),$row->author)); ?>
							</span>
							<?php endif; ?>
							<?php if( $showDate ): ?>
							<span class="bt-date"><?php echo JText::sprintf('BT_CREATEDON', $row->date); ?>
							</span>
							<?php endif; ?>
						</div>
						<?php endif; ?>

						<?php if( $show_intro ): ?>
						<div class="bt-introtext">
						<?php echo $row->description; ?>
						</div>
						<?php endif; ?>

						<?php if( $showReadmore ) : ?>
						<p class="readmore">
							<a target="<?php echo $openTarget; ?>"
								title="<?php echo $row->title;?>"
								href="<?php echo $row->link;?>"> <?php echo JText::_('READ_MORE');?>
							</a>
						</p>
						<?php endif; ?>

					</div>
					<!-- bt-inner -->

				</div>
				<!-- bt-row -->
                                <?php
                                if($vertical){
                                    
                                    if((($i+1)%$itemsPerRow ==0 && $itemsPerRow > 1) || $i == count($list)-1){
						echo '</li>';
                                    }
                                }else{
                                    if(($i+1) % $itemsPerCol == 0 || $i == count($list)-1){	
                                        
                                        echo '</li>';
                                    }
				
                                }
				?>

				<?php endforeach; ?>
                        <?php endforeach; ?>
                    </ul>
		
		</div>


	</div>
	<!-- bt-container -->


</div>
			<?php else : ?>
<div>No result...</div>
			<?php endif; ?>
<div style="clear: both;"></div>


<?php
$nav_top = (-1)*(int)$params->get( 'navigation_top', 0 ) ; 
$nav_right = (-1)*(int)$params->get( 'navigation_right', 0 );
?>
<?php if( $totalPages  > 1 ): ?>
    <script type="text/javascript">
        jQuery(document).ready(function(){
            jQuery('#btcontentshowcase<?php echo $module->id; ?> .jcarousel').jcarousel({
                <?php if($params->get('pause_hover')){ ?>
                        initCallback: btContentShowcaseCallback,
                <?php }?>        
                start: 1,
                auto: <?php echo ($params->get('auto_start')) ? $params->get('interval', 5000) : '0' ?>,
                animation: <?php echo (int)$params->get('duration', '1000')?>,
                <?php if(!$params->get('next_back', 1)){?>
                    buttonNextHTML: null,
                    buttonPrevHTML: null,
                <?php }?>
                scroll : <?php echo $params->get('slide_item_per_time', 1) ?>,
                vertical: <?php echo ($vertical) ? 'true' : 'false' ?>,
                wrap : 'both'
            });
        });
        <?php if($params->get('pause_hover')){ ?>
        function btContentShowcaseCallback(showcase){
            showcase.clip.hover(function() {
                showcase.stopAuto();
            }, function() {
                showcase.startAuto();
            });
        }
        <?php } ?>
    </script>
	<?php if(!$params->get( 'butlet', 0 )) $totalPages = 0; ?>
	<style>
            #btcontentshowcase<?php echo $module->id; ?> .jcarousel li{
                width: <?php echo $liWidth ?>px;
                height: <?php echo $liHeight ?>px;
            }
            
            <?php if($vertical){?>

            #btcontentshowcase<?php echo $module->id; ?> .jcarousel-skin-tango .jcarousel-clip-vertical {

                height: <?php echo ($params->get('item_height')) ? (($params->get('item_height') +20) * $itemsPerCol) . 'px' : 'auto';?>;
            }
            <?php }?>
            <?php if(abs($nav_top) == 0 &&  trim($params->get('content_title') =="" )){ ?>
            #btcontentshowcase<?php echo $module->id; ?>{
                    padding-top:32px;
            }
            <?php } ?>
            #btcontentshowcase<?php echo $module->id; ?> .jcarousel-next{
                    top:<?php echo $nav_top - 35 ?>px!important;
                    right:<?php echo $nav_right + 14 ?>px!important;
            }
            #btcontentshowcase<?php echo $module->id; ?> .jcarousel-prev{
                    top:<?php echo $nav_top - 35 ?>px!important;
                    right:<?php echo $nav_right + 30 ?>px!important;
            }

            <?php if($params->get('item_height')){?>
            #btcontentshowcase<?php echo $module->id; ?> .bt-inner{
                height: <?php echo $params->get('item_height')?>px;
            }
            <?php }?>

            <?php if($align_image != 'center'){?>
            #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-title,
            #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-title-nointro,
            #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-category,
            #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-introtext,
            #btcontentshowcase<?php echo $module->id; ?> .bt-inner .bt-extra{
                margin-<?php echo $align_image ?>: <?php echo ($thumbWidth + 10)?>px;
            }
            <?php
            }
            ?>
    </style>
<?php else: ?>
<script type="text/javascript">	
	(function(){
		jQuery('#btcontentshowcase<?php echo $module->id; ?>').fadeIn("fast");
	})();
</script>
<?php endif; ?>